package com.situ.mall.controller;


import com.situ.mall.pojo.entity.Shipping;
import com.situ.mall.service.IShippingService;
import com.situ.mall.util.JwtUtil;
import com.situ.mall.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Gao
 * @since 2025-10-15
 */
@RestController
@RequestMapping("/user/shipping")
public class ShippingController {
    @Autowired
    private IShippingService shippingService;

    @GetMapping("/selectByUserId")
    public Result<List<Shipping>> selectByUserId(@RequestHeader(name = "Authorization")String token) {
        Map<String, Object> tokenMap = JwtUtil.parseToken(token);
        Integer userId = (Integer) tokenMap.get("id");
        List<Shipping> list = shippingService.selectByUserId(userId);
        return Result.ok(list);
    }

    @PutMapping("/updateStatus")
    public Result updateStatus(@RequestBody Shipping shipping, @RequestHeader(name = "Authorization")String token) {
        Map<String, Object> tokenMap = JwtUtil.parseToken(token);
        Integer userId = (Integer) tokenMap.get("id");
        shipping.setUserId(userId);
        shippingService.updateStatus(shipping);
        return Result.ok("更新成功");
    }

    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        shippingService.removeById(id);
        return Result.ok("删除成功");
    }

    @PostMapping("/add")
    public Result add(@RequestBody Shipping shipping, @RequestHeader(name = "Authorization")String token) {
        Map<String, Object> tokenMap = JwtUtil.parseToken(token);
        Integer userId = (Integer) tokenMap.get("id");
        shipping.setUserId(userId);
        shippingService.save(shipping);
        return Result.ok("添加成功");
    }
}

